package leetcode.p1424;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: trtan
 * @create: 2021-04-13 19:28
 * @description:
 **/
public class DiagonalTraverseTwo {
    public int[] findDiagonalOrder(List<List<Integer>> nums) {
        if (nums.size() == 0) {
            return new int[0];
        }
        List<Point> points = new ArrayList<>();
        int len = 0;
        int i = 0;
        for (List<Integer> list : nums) {
            int j = 0;
            for (Integer val : list) {
                points.add(new Point(val, i, i + j));
                j++;
                len++;
            }
            i++;
        }
        int[] array = new int[len];
        points.sort((a, b)->{
            if (a.sum == b.sum) {
                return b.i  - a.i;
            }
            return a.sum - b.sum;
        });
        i = 0;
        for (Point p : points) {
            array[i++] = p.val;
        }
        return array;
    }
    class Point {
        int val, i, sum;
        Point(int val, int i, int sum) {
            this.val = val;
            this.i = i;
            this.sum = sum;
        }
    }
}
